package me.wangshier.leetcode.easy;

/**
 * @Author: tongqianwen
 * @Date: 2025/3/2
 */
public class NumericStringFormatting {

    public static String solution(String s) {
        // 去除前导零
        s = s.replaceFirst("^0+(?!$)", "");

        // 分割整数和小数部分
        String[] parts = s.split("\\.");
        String integerPart = parts[0];
        String decimalPart = parts.length > 1 ? "." + parts[1] : "";

        // 处理整数部分
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < integerPart.length(); i++) {
            if (i > 0 && (integerPart.length() - i) % 3 == 0) {
                // 在这里插入逗号
                sb.append(",");
            }
            // 追加当前字符
            sb.append(integerPart.charAt(i));
        }

        // 合并结果
        return sb.toString() + decimalPart;
    }

    public static void main(String[] args) {
        System.out.println(solution("000000"));
        System.out.println(solution("0.0012"));
        System.out.println(solution("1294512.12412").equals("1,294,512.12412"));
        System.out.println(solution("0000123456789.99").equals("123,456,789.99"));
        System.out.println(solution("987654321").equals("987,654,321"));
    }
}
